package org.wadael.waverobotfrmwrk.utils;

import org.wadael.waverobotfrmwrk.WadRobot;
import org.wadael.waverobotfrmwrk.advanced.Command;
import org.wadael.waverobotfrmwrk.advanced.WithDelimitedCommandsRobot;

import com.google.wave.api.Blip;
import com.google.wave.api.BlipContentRefs;
import com.google.wave.api.Wavelet;
import com.google.wave.api.event.Event;

/**
 * Collection of methods to manipulate blips. Mainly static methods. Mostly
 * convenience methods to avoid repeating in one's code.
 * 
 * @author jerome (wadael)
 * 
 */
public class BlipUtils {

	/**
	 * Replaces the content of a blip
	 * 
	 * @param event
	 *            a BLipSubmittedEvent or .... instance
	 * @param nextContent
	 */
	public static void replaceBlipContent(Blip bleep, String nextContent) {
		BlipContentRefs bcr = bleep.all();
		if (bcr != null) {
			bcr.delete();
			bleep.append(nextContent);
		}
	}

	/**
	 * 
	 * @param event
	 * @param nextContent
	 */
	public static void replaceBlipContent(Event event, String nextContent) {
		Blip blee = event.getBlip();
		replaceBlipContent(blee, nextContent);
	}

	/**
	 * Appends a new blip after the blip of the event, with the given string as
	 * content
	 * 
	 * @param event
	 * @param newContent
	 */
	public static void appendNewBlipWithContent(Event event, String newContent) {
		// BlipContentRefs bcrs =
		// event.getBlip().getParentBlip().append(newContent);
		// event.getBundle().
		if (event != null)
			event.getBlip().reply().append(newContent);
	}

	/**
	 * Returns the text of the blip that triggered the event passed as
	 * parameter.
	 * 
	 * @param event
	 * @return
	 */
	public static String getEventBlipText(Event event) {
		if (event != null)
			return event.getBlip().getContent();
		else
			return null;
		// TODO are NPE possible ?
	}

	/**
	 * Removes the command after usage. Nice.
	 * 
	 * @param c
	 * @param blip
	 * @param rw
	 *            RobotWorker
	 */
	@SuppressWarnings("deprecation")
	public static void removeCommandFromBlip(Command c, Blip blip,
			WithDelimitedCommandsRobot rw) {
		replaceCommandFromBlip(c, blip, rw, "");
	}

	/**
	 * Opens the way to commands that get replaced by a value (after
	 * validation).
	 * 
	 * @param c
	 * @param blip
	 * @param rw
	 * @param replacingText
	 *            the text that will replace the command
	 */
	public static void replaceCommandFromBlip(Command c, Blip blip,
			WithDelimitedCommandsRobot rw, String replacingText) {
		String toRemove = rw.getStartCommandDelimiter() + c.getAcronym()
				+ rw.getAcronymAndCommandSeparator() + c.getInstruction()
				+ ((c.getParameters() == null) ? "" : c.getParameters())
				+ rw.getEndCommandDelimiter();
		String nextContent = blip.getContent().replace(toRemove, replacingText);
		replaceBlipContent(blip, nextContent);
	}

	/**
	 * Creates a child blip with content.
	 * 
	 * TODO correct : is not a child : how to add a child ?
	 * 
	 * @param blip
	 * @param childContent
	 */
	public static void createChildBlip(Blip blip, String childContent) {
		Blip shild = blip.reply();
		replaceBlipContent(shild, childContent);
	}

	/**
	 * 
	 * @param wavelet
	 * @param user
	 * @return
	 */
	public static boolean isWaveletCreator(Wavelet wavelet, String user) {
		if (wavelet == null || user == null) return false;
		return wavelet.getCreator().equals(user);
	}
	
	/**
	 * Returns true if the blip given as paremeter is issued by the wavelet creator 
	 * @param blip
	 * @return
	 */
	public static boolean isFromWaveletCreator(Event event) {
		if( event==null) return false;
		else{
			return event.getWavelet().getCreator().equals(event.getModifiedBy() ) ;
		}
	}

	/**
	 * Returns true if the user that triggered the event is an authorized user of the wadrobot  
	 * @param event
	 * @param robot
	 * @return
	 */
	public static boolean isUserAnAuthorizedUser(Event event, WadRobot robot) {
		if( event == null || robot == null) return false;
		return robot.getAuthorisedUsers().contains(event.getModifiedBy());
	}

}
